//
//  CNUILabel.swift
//  Voice
//
//  Created by 卢逢卫 on 2024/2/19.
//

import UIKit

public class CNUILabel: UILabel {

    public enum CNUILabelType {
        case middleLine
        case bottomLine
        case moreColor
        case moreFont
        case moreColorAndFont
        case normal
    }

    public var labelText: String? {
        didSet {
            self.text = labelText
            updateLabel()
        }
    }

    public var lineColor: UIColor?
    
    public var colorStartLocation: Int?
    public var colorLength: Int?
    public var colorValue: UIColor?
    
    public var fontStartLocation: Int?
    public var fontLength: Int?
    public var fontValue: UIFont?

    public var labelType: CNUILabelType = .normal {
        didSet {
            updateLabel()
        }
    }
    
    public init(frame: CGRect = .zero, labelType: CNUILabelType = .normal) {
        self.labelType = labelType
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    private func updateLabel() {
        guard let text = self.text else { return }

        let attributedString = NSMutableAttributedString(string: text)
        
        switch labelType {
        case .middleLine, .bottomLine:
            if let lineColor = lineColor {
                let range = NSMakeRange(0, text.count)
                let style = (labelType == .middleLine) ? NSUnderlineStyle.single.rawValue : NSUnderlineStyle.patternDash.rawValue
                attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: style, range: range)
                attributedString.addAttribute(NSAttributedString.Key.strikethroughColor, value: lineColor, range: range)
            }

        case .moreColor:
            if let colorStartLocation = colorStartLocation, let colorLength = colorLength, let colorValue = colorValue {
                attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: colorValue, range: NSMakeRange(colorStartLocation, colorLength))
            }

        case .moreFont:
            if let fontStartLocation = fontStartLocation, let fontLength = fontLength, let fontValue = fontValue {
                attributedString.addAttribute(NSAttributedString.Key.font, value: fontValue, range: NSMakeRange(fontStartLocation, fontLength))
            }

        case .moreColorAndFont:
            if let colorStartLocation = colorStartLocation, let colorLength = colorLength, let colorValue = colorValue {
                attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: colorValue, range: NSMakeRange(colorStartLocation, colorLength))
            }
            if let fontStartLocation = fontStartLocation, let fontLength = fontLength, let fontValue = fontValue {
                attributedString.addAttribute(NSAttributedString.Key.font, value: fontValue, range: NSMakeRange(fontStartLocation, fontLength))
            }

        case .normal:
            break
        }

        self.attributedText = attributedString
    }
}
